using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using Microsoft.Phone.Info;
using System;
using System.Net;
using System.Net.Sockets;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class NetSceneHttpDownload
	{
		private const string TAG = "NetSceneHttpDownload";
		private const int MAX_COUNT = 100;
		private int nFireCount;
		private HttpClient mHttpClient;
		private DownloadMediaContext mContext;
		public void getRequest(DownloadMediaContext context)
		{
			this.mContext = context;
			this.mContext.mSnsMediaInfo.status = 1;
			this.mHttpClient = new HttpClient(this.mContext.mSnsMediaInfo.string_0, 1, null);
			this.mHttpClient.mDefineHeader += new DefineHttpHeaderDelegate(this.onDefineHttpHeader);
			this.mHttpClient.send(null, 0, new Action<byte[]>(this.OnHttpRespone));
		}
		public void onDefineHttpHeader(HttpWebRequest request)
		{
			request.set_Method("GET");
			request.set_UserAgent(string.Concat(new object[]
			{
				"wp7 ",
				DeviceStatus.get_DeviceName(),
				"; ",
				DeviceStatus.get_DeviceManufacturer(),
				"; ",
				Environment.get_OSVersion().get_Version(),
				" ;",
				AppInfoHelper.Version,
				"; ",
				NetworkDeviceWatcher.getCurrentNetworkType()
			}));
			request.get_Headers().set_Item("Connection", "Keep-Alive");
			request.set_Accept("*/*");
		}
		public void OnHttpRespone(byte[] data_buf)
		{
			SnsPageMgr.setTimeStamp("receive  image buffer");
			if (data_buf == null)
			{
				Log.smethod_1("NetSceneHttpDownload", "http request uri faile,cmduri = " + this.mHttpClient.mHttpContext.mCmdUri);
				return;
			}
			FlowControl.onReceive(0, null, (uint)data_buf.Length);
			if (!SnsMediaMgr.save(this.mContext.mSnsMediaInfo, data_buf))
			{
				this.mContext.mSnsMediaInfo.object_0 = null;
				EventCenter.postEvent(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, this.mContext.mSnsMediaInfo, null);
			}
			else
			{
				EventCenter.postEvent(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, this.mContext.mSnsMediaInfo, null);
			}
			this.mContext.mSnsMediaInfo.status = 3;
			NetSceneHttpDownloadService.onRequestFinished(this);
			DownloadMediaService.onContextDispatcher(null, null);
			Log.smethod_4("Network", "request finished cmduri = " + this.mHttpClient.mHttpContext.mCmdUri);
		}
		public void OnRespone()
		{
			byte[] array = null;
			int num = 0;
			string text = null;
			SocketError socketError = this.mHttpClient.receive(ref array, ref num, ref text);
			if (socketError == 11004 && this.nFireCount < 100)
			{
				this.nFireCount++;
				return;
			}
			if (this.nFireCount < 100 && socketError == null)
			{
				FlowControl.onReceive(0, null, (uint)array.Length);
				if (!SnsMediaMgr.save(this.mContext.mSnsMediaInfo, array))
				{
					this.mContext.mSnsMediaInfo.object_0 = null;
					EventCenter.postEvent(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, this.mContext.mSnsMediaInfo, null);
				}
				else
				{
					EventCenter.postEvent(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, this.mContext.mSnsMediaInfo, null);
				}
				this.mContext.mSnsMediaInfo.status = 3;
				Log.smethod_4("Network", "request finished cmduri = " + text);
			}
			else
			{
				Log.smethod_1("NetSceneHttpDownload", "request timeout cmduri = " + text);
				this.mContext.mSnsMediaInfo.retryTimes++;
				this.mContext.mSnsMediaInfo.status = 4;
				DownloadMediaContextMgr.Instance.moveToTail(this.mContext);
				if (this.mContext.mSnsMediaInfo.retryTimes > 1)
				{
					this.mContext.mSnsMediaInfo.object_0 = null;
					EventCenter.postEvent(EventConst.ON_MGR_SNS_GET_MEDIA_FINISH, this.mContext.mSnsMediaInfo, null);
				}
			}
			NetSceneHttpDownloadService.onRequestFinished(this);
			DownloadMediaService.onContextDispatcher(null, null);
		}
	}
}
